package com.hele.sts.action.admin;

import java.io.DataOutputStream;
import java.io.FileOutputStream;

import com.opensymphony.xwork2.Action;

public class GenCtxXmlFileAction
  implements Action
{
  private String sid;
  private String ipAddr;
  private String port;
  private String userId;
  private String userPasswd;
  private String path;
  private String errorInfo;
  
  public String getErrorInfo()
  {
    return this.errorInfo;
  }
  
  public void setErrorInfo(String errorInfo)
  {
    this.errorInfo = errorInfo;
  }
  
  public String getIpAddr()
  {
    return this.ipAddr;
  }
  
  public void setIpAddr(String ipAddr)
  {
    this.ipAddr = ipAddr;
  }
  
  public String getPort()
  {
    return this.port;
  }
  
  public void setPort(String port)
  {
    this.port = port;
  }
  
  public String getSid()
  {
    return this.sid;
  }
  
  public void setSid(String sid)
  {
    this.sid = sid;
  }
  
  public String getUserId()
  {
    return this.userId;
  }
  
  public void setUserId(String userId)
  {
    this.userId = userId;
  }
  
  public String getUserPasswd()
  {
    return this.userPasswd;
  }
  
  public void setUserPasswd(String userPasswd)
  {
    this.userPasswd = userPasswd;
  }
  
  public String getPath()
  {
    return this.path;
  }
  
  public void setPath(String path)
  {
    this.path = path;
  }
  
  public String execute()
    throws Exception
  {
    DataOutputStream bfo = null;
    this.errorInfo = "生成数据库连接配置文件失败！";
    try
    {
      StringBuffer strBuff = new StringBuffer();
      strBuff
        .append("<!-- The contents of this file will be loaded for each web application -->\r\n");
      strBuff.append("<Context>\r\n");
      strBuff.append("\t<!-- Default set of monitored resources -->\r\n");
      strBuff
        .append("\t<WatchedResource>WEB-INF/web.xml</WatchedResource>\r\n");
      strBuff.append("\t\r\n");
      strBuff
        .append("\t<!-- Uncomment this to disable session persistence across\r\n");
      strBuff.append("\tTomcat restarts -->\r\n");
      strBuff.append("\t<!--\r\n");
      strBuff.append("\t<Manager pathname=\"\" />\r\n");
      strBuff.append("\t-->\r\n");
      strBuff.append("\t<Resource\r\n");
      strBuff.append("\t\tname=\"jdbc/myoracle\"\r\n");
      strBuff.append("\t\ttype=\"javax.sql.DataSource\"\r\n");
      strBuff.append("\t\tmaxActive=\"100\"\r\n");
      strBuff.append("\t\tmaxIdle=\"10\"\r\n");
      
      strBuff.append("\t\tusername=\"");
      strBuff.append(this.userId);
      strBuff.append("\"\r\n");
      
      strBuff.append("\t\tmaxWait=\"-1\"\r\n");
      strBuff
        .append("\t\tdriverClassName=\"oracle.jdbc.driver.OracleDriver\"\r\n");
      strBuff.append("\t\tpassword=\"");
      strBuff.append(this.userPasswd);
      strBuff.append("\"\r\n");
      strBuff.append("\t\turl=\"jdbc:oracle:thin:@");
      strBuff.append(this.ipAddr);
      strBuff.append(":");
      strBuff.append(this.port);
      strBuff.append(":");
      strBuff.append(this.sid);
      strBuff.append("\"");
      strBuff.append("/>\r\n");
      strBuff.append("</Context>");
      bfo = new DataOutputStream(new FileOutputStream(this.path + "/context.xml"));
      bfo.write(strBuff.toString().getBytes());
      this.errorInfo = "文件已生成！";
      return "success";
    }
    catch (Exception ex)
    {
      System.out.println(ex.toString());
    }
    return "error";
  }
}
